﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Text;
using System.IO;
using System.Data;
using System.Text.RegularExpressions;
using System.Collections;
/// <summary>
///SetConnLink 的摘要说明
/// </summary>
public static class SetConnLink
{

    /// <summary>
    /// 去掉文章中的链接
    /// </summary>
    /// <param name="_Html"></param>
    /// <returns></returns>
    public static string GetNoneLink(string _Html)
    {
        _Html = Regex.Replace(_Html, "<a[^>]*href=[\'\"\\s]*([^\\s\'\"]*)[^>]*>(.+?)<\\/a>", "$2");
        return _Html;
    }
    /// <summary>
    /// 给关键字加链接，同一关键字只加一次 慢的要命。。
    /// </summary>
    /// <param name="src">源字符串</param>
    /// <param name="keys">关键字泛型</param>
    /// <returns>替换后结果</returns>
    public static string FormatBody(string src)
    {
        List<string> keys = SetLink();//获取关键字数组
        Hashtable KeyUrl = GetLinkHS();

        Regex reg = new Regex(@"(?i)(?:^|(?<!<a\b(?>[^<>]*))>)(?>[^<>]*)(?:<|$)");
        int length = 0;
        string temp = string.Empty;
        return reg.Replace(src, delegate(Match m)
        {
            temp = m.Value;
            length = temp.Length;
            for (int i = keys.Count - 1; i >= 0; i--)
            {
                temp = Regex.Replace(temp, @"(?is)^((?:(?:(?!" + Regex.Escape(keys[i]) + @"|</?a\b).)*<a\b(?:(?!</?a\b).)*</a>)*(?:(?!" + Regex.Escape(keys[i]) + @"|</?a\b).)*)(?<tag>" + Regex.Escape(keys[i]) + @")", @"$1<a href='" + KeyUrl[keys[i]].ToString() + "'>${tag}</a>");
                if (length != temp.Length)
                {
                    keys.Remove(keys[i]);
                }
                length = temp.Length;
            }
            return temp;
        });
    }
    ///// <summary>
    ///// 获取关键字
    ///// </summary>
    ///// <returns></returns>
    public static Hashtable GetLinkHS()
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbDataAdapter drr = new OleDbDataAdapter("Select * from [KeyWord]", myconn);
        DataSet ds = new DataSet();
        try
        {
            drr.Fill(ds);

        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        Hashtable tags = new Hashtable();
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            string s = dr["keyword"].ToString();
            tags.Add(dr["keyword"].ToString(), dr["geturl"].ToString());
        }
        return tags;
    }
    public static List<string> SetLink()
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbDataAdapter drr = new OleDbDataAdapter("Select * from [KeyWord]", myconn);
        DataSet ds = new DataSet();
        try
        {
            drr.Fill(ds);
        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        List<string> tags = new List<string>();
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            tags.Add(dr.ItemArray[1].ToString());
        }
        return tags;
    }

}